<template>
  <a-modal
    width="1000"
    title="购货单"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @cancel="visible = false"
    @ok="visible = false"
  >
    <a-form @keyup.enter.native="searchQuery">
      <a-row :gutter="24">
        <a-col :md="6" :sm="24">
          <a-form-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入单据编号" v-model="queryParam.orderNo"></a-input>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-range-picker
              style="width: 100%"
              v-model="queryParam.createTimeRange"
              format="YYYY-MM-DD"
              :placeholder="['开始时间', '结束时间']"
              @change="onDateChange"
            />
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select placeholder="请选择供应商" showSearch optionFilterProp="children" v-model="queryParam.supplierId">
              <a-select-option v-for="(item, index) in supList" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select placeholder="请选择业务员" showSearch optionFilterProp="children" v-model="queryParam.salesmanId">
              <a-select-option v-for="(item, index) in userList" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select placeholder="请选择审核员" showSearch optionFilterProp="children" v-model="queryParam.auditorId">
              <a-select-option v-for="(item, index) in userList" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="6" :sm="24">
                  <a-form-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select placeholder="请选择单据状态" v-model="queryParam.isAudited">
                      <a-select-option value="0">未审核</a-select-option>
                      <a-select-option value="1">已审核</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
        <a-col :md="6" :sm="24">
          <a-form-item label="付款状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select placeholder="请选择单据状态" v-model="queryParam.paymentStatus">
                      <a-select-option value="已付款">已付款</a-select-option>
                      <a-select-option value="未付款">未付款</a-select-option>
                      <a-select-option value="部分付款">部分付款</a-select-option>
                    </a-select>
                  </a-form-item>
        </a-col>
        <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
          <a-button type="primary" @click="searchQuery">查询</a-button>
          <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
        </span>
      </a-row>
    </a-form>

    <a-table size="middle" bordered rowKey="id" :columns="columns" :dataSource="purchaseOrderList">
      <template slot="action" slot-scope="text, record">
        <a slot="action" @click="handleChoose(record)">选择</a>
      </template>
      <span slot="isAudited" slot-scope="text, record">
        <a-badge
          :status="record.isAudited === 1 ? 'success' : 'error'"
          :text="record.isAudited === 1 ? '已审核' : '未审核'"
        />
      </span>
    </a-table>
  </a-modal>
</template>

<script>
import { getPurchaseOrderOnly } from '@/api/api'
import { supplierAll, userAll } from '@/api/api'
import { getFormatDate, getPrevMonthFormatDate } from '@/utils/util'
import moment from 'moment'
export default {
  data() {
    return {
      supList: [],
      userList: [],
      labelCol: {
        span: 5,
      },
      wrapperCol: {
        span: 18,
        offset: 1,
      },
      queryParam: {
        beginTime: getPrevMonthFormatDate(3),
        endTime: getFormatDate(),
        createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())],
      },
      purchaseOrderList: [],
      visible: false,
      confirmLoading: false,
      columns: [
 {
          title: '单据编号',
          dataIndex: 'orderNo',
          width: 160,
        },
        { title: '单据日期', dataIndex: 'orderDate', width: 120 },
        { title: '购货价格', dataIndex: 'purchaseAmount', width: 60 },
        { title: '优惠金额', dataIndex: 'discountAmount', width: 60 },
        { title: '数量', dataIndex: 'quantity', width: 60 },
        { title: '折后金额', dataIndex: 'amountAfterDiscount', width: 60 },
        { title: '已付款金额', dataIndex: 'paidAmount', width: 60 },
        { title: '送货方式', dataIndex: 'deliveryMethod', width: 60 },
        { title: '付款状态', dataIndex: 'paymentStatus', width: 100 },
        { title: '退货状态', dataIndex: 'returnStatus', width: 60 },
        { title: '审核状态', dataIndex: 'isAudited', width: 80, scopedSlots: { customRender: 'isAudited' } },
        { title: '供应商', dataIndex: 'supplierName', width: 160 },
        { title: '业务员', dataIndex: 'salesmanName', width: 60 },
        { title: '退款单编号', dataIndex: 'purchaseReturnOrderNo', width: 60 },
        { title: '结算账户', dataIndex: 'settlementAccountName', width: 120 },
        { title: '审核人', dataIndex: 'auditorName', width: 60 },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 30,
          scopedSlots: { customRender: 'action' },
        },
      ],
    }
  },
  mounted() {},
  methods: {
    handleChoose(record) {
      this.$emit('choose', record)
      this.visible = false
    },
    show() {
      this.visible = true
      this.initUser()
      this.initSupplier()
      this.searchReset()
    },
    searchQuery() {
      console.log(this.queryParam)
      getPurchaseOrderOnly(this.queryParam).then((res) => {
        if (res.code === 200 && res.data) {
          this.purchaseOrderList = res.data
        }
      })
    },
    searchReset() {
      this.queryParam = {
        beginTime: getPrevMonthFormatDate(3),
        endTime: getFormatDate(),
        createTimeRange: [moment(getPrevMonthFormatDate(3)), moment(getFormatDate())],
      }
      this.searchQuery()
    },
    onDateChange: function (value, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
      if (dateString[0] && dateString[1]) {
        this.queryParam.createTimeRange = [moment(dateString[0]), moment(dateString[1])]
      }
    },
    initUser() {
      userAll({}).then((res) => {
        if (res.data) {
          this.userList = res.data
        }
      })
    },
    initSupplier() {
      supplierAll({}).then((res) => {
        if (res) {
          this.supList = res.data
        }
      })
    },
  },
}
</script>

<style>
</style>